<?php 
// MYSQL Helper Function

global $connection;

function updateCounterByOne($t) {
	global $counters;
	$counters[$t] = (isset($counters[$t])) ? $counters[$t]+1 : 1;
}

function getConnection() {
	global $connection;
	$connection = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
 
	// check connection
	if ($connection->connect_error) {
	  trigger_error('Database connection failed: '  . $connection->connect_error, E_USER_ERROR);
	}
}

function executeUpdateQuery_old($query) {
	global $connection;
	updateCounterByOne('UPDATE');
	$result = mysql_query($query);
	if(mysql_errno($connection)!=0) {
		$error =  mysql_errno($connection) . ": " . mysql_error($connection). "\n";
		dump_var("MYSQL - Error",$error);
		writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	}
	dump_var($query,"");
	return mysql_affected_rows($connection);
}

function executeUpdateQuery($query) {
	global $connection;
	updateCounterByOne('UPDATE');
	$result =$connection->query($query);
	if(!$result || $result==NULL) {
		$error =  mysqli_errno($connection) . ": " . $connection->error. "\n";
	  dump_var("MYSQL - Error",$error);
	  writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	}
	dump_var($query,"");
	return $connection->affected_rows;
}


function executeWriteQuery_OLD($query) {
	global $connection;
	updateCounterByOne('WRITE');
	$result = mysql_query($query);
	if(mysql_errno($connection)!=0) {
		$error =  mysql_errno($connection) . ": " . mysql_error($connection). "\n";
		dump_var("MYSQL - Error",$error);
		writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	}
	dump_var($query,"");
	return mysql_insert_id($connection);
}

function executeWriteQuery($query) {
	global $connection;
	updateCounterByOne('WRITE');
	$result =$connection->query($query);
	if(!$result || $result==NULL) {
		$error =  mysqli_errno($connection) . ": " . $connection->error. "\n";
	  dump_var("MYSQL - Error",$error);
	  writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	}
	dump_var($query,"");
	// returns 0 in not inserted
	return $connection->insert_id;
}

function executeReadQuery_old($query) {
	global $connection;
	updateCounterByOne('READ');
	$result = mysql_query($query);
	if(mysql_errno($connection)!=0) {
		$error =  mysql_errno($connection) . ": " . mysql_error($connection). "\n";
		dump_var("MYSQL - Error",$error);
		writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	}
	$r = [];
	if(mysql_num_rows($result)>0) {
		while($row = mysql_fetch_object($result)) {
			$r[] = $row;
		}
	}
	dump_var($query,$r);
	return $r;
}

function executeReadQuery($query) {
	global $connection;
	updateCounterByOne('READ');
	$rs=$connection->query($query);
	$arr=NULL;
	if(!$rs || $rs==NULL) {
	  $error =  mysqli_errno($connection) . ": " . $connection->error. "\n";
	  dump_var("MYSQL - Error",$error);
	  writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	} else {
	   $arr = $rs->fetch_all(MYSQLI_ASSOC);
	}
	dump_var($query,$arr);
	return $arr;
}

function executeDeleteQuery($query){
	global $connection;
	updateCounterByOne('DELETE');
	$result = mysql_query($query);
	if(mysql_errno($connection)!=0) {
		$error =  mysql_errno($connection) . ": " . mysql_error($connection). "\n";
		dump_var("MYSQL - Error",$error);
		writeToLogFile("MYSQL - Error\t\t".$error."while executing $query");
	}
	dump_var($query,"");
	return mysql_affected_rows($connection);
}

function closeConnection() {
	global $connection;
	if(!$connection->close()) die("Unable to close connection");
}

?>
